package com.ruoyi.web.controller.finance;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.finance.domain.Invoices;
import com.ruoyi.finance.service.IInvoicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 发票Controller
 * 
 * @author liuyu
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/invoice/invoice")
public class InvoicesController extends BaseController
{
    @Autowired
    private IInvoicesService invoicesService;

    /**
     * 查询发票列表
     */
    @PreAuthorize("@ss.hasPermi('invoice:invoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(Invoices invoices)
    {
        startPage();
        List<Invoices> list = invoicesService.selectInvoicesList(invoices);
        return getDataTable(list);
    }

    /**
     * 导出发票列表
     */
    @PreAuthorize("@ss.hasPermi('invoice:invoice:export')")
    @Log(title = "发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Invoices invoices)
    {
        List<Invoices> list = invoicesService.selectInvoicesList(invoices);
        ExcelUtil<Invoices> util = new ExcelUtil<Invoices>(Invoices.class);
        util.exportExcel(response, list, "发票数据");
    }

    /**
     * 获取发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('invoice:invoice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(invoicesService.selectInvoicesById(id));
    }

    /**
     * 新增发票
     */
    @PreAuthorize("@ss.hasPermi('invoice:invoice:add')")
    @Log(title = "发票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Invoices invoices)
    {
        System.out.println(invoices);
        return toAjax(invoicesService.insertInvoices(invoices));
    }

    /**
     * 修改发票
     */
    @PreAuthorize("@ss.hasPermi('invoice:invoice:edit')")
    @Log(title = "发票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Invoices invoices)
    {
        return toAjax(invoicesService.updateInvoices(invoices));
    }

    /**
     * 删除发票
     */
    @PreAuthorize("@ss.hasPermi('invoice:invoice:remove')")
    @Log(title = "发票", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invoicesService.deleteInvoicesByIds(ids));
    }
}